<template>
  <div class="app-container">
    <el-form :inline="true" :model="searchForm" size="mini">
      <el-form-item>
        <el-button type="primary" size="mini" @click="showForm(false,null)">新增</el-button>
      </el-form-item>
      <el-form-item label="功能名称">
        <el-input v-model="searchForm.accessName" placeholder="请输入字典组"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="loadData">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="tableData" stripe style="width: 100%" border size="mini" row-key="id" lazy :load="loadChildren">
      <el-table-column prop="accessName" label="功能名称" align="center"/>
      <el-table-column prop="accessUrl" label="url" align="center"/>
      <el-table-column prop="component" label="组件" align="center"/>
      <el-table-column prop="menuLevel" label="菜单等级" align="center">
        <template slot-scope="scope">
          <span>{{scope.row.menuLevel === 0 ? '按钮':scope.row.menuLevel+'级'}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="menuIcon" label="图标" align="center"/>
      <el-table-column prop="sortNum" label="排序" align="center"/>
      <el-table-column label="操作" align="center" width="120">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="showForm(true,scope.row)">编辑</el-button>
          <el-button type="text" size="small" @click="delRow(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <SysMenuForm ref="SysMenuForm" @refreshTable="loadData"/>
  </div>
</template>

<script>
import {listRoleAccessByParentApi,deleteApi} from '@/api/system/sysMenu'
import SysMenuForm from '@/views/system/form/SysMenuForm'
export default {
  name: 'SysMenu',
  components: { SysMenuForm },
  data() {
    return {
      searchForm:{accessName:''},
      //pageConfig:{ pageNo:1, pageSize:10, total:0 },
      tableData:[],
    }
  },
  created() {
    this.loadData();
  },
  methods: {
    loadChildren(row, treeNode, resolve){
      listRoleAccessByParentApi({parentId:row.id}).then(res => {
        resolve(res.data)
      })
    },
    loadData(){
      this.tableData = []
      listRoleAccessByParentApi(this.searchForm).then(res => {
        this.tableData = res.data;
      })
    },
    showForm(isEdit,row){
      let rowData = {}
      if(isEdit){
        rowData = JSON.parse(JSON.stringify(row))
      }
      this.$refs.SysMenuForm.opened(isEdit, rowData);
    },
    delRow(row){
      this.$confirm('此操作将永久删除该内容, 是否继续?', '提示', {
        confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning'
      }).then(() => {
        deleteApi({id:row.id}).then(res => {
          this.$message.success('删除成功!');
          this.loadData();
        })
      }).catch(() => {
        this.$message.info('已取消删除');
      });
    }
  }
}
</script>
